package com.lagou.product.service;

import com.lagou.product.mapper.ProductMapper;
import com.lagou.product.mq.Producer;
import com.lagou.product.pojo.Product;
import com.lagou.product.pojo.ProductQuery;
import com.lagou.product.pojo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.RequestEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@Service
public class ProductServiceImpl implements ProductService{

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private Producer producer;

    @Override
    public Product saveProduct(Product product) {


        Long id = productMapper.insertProduct(product);

        System.out.println("发送id: " +product.getId());

        producer.send(product.getId());

        return product;
    }

    @Override
    public List<ProductVo> queryProduct(ProductQuery productQuery) {

        RestTemplate restTemplate = new RestTemplate();

        MultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<>();

        String url = "http://127.0.0.1:8888/product/query";

        paramMap.add("page",productQuery.getPage());
        paramMap.add("name", productQuery.getName());
        paramMap.add("minPrice", productQuery.getMinPrice());
        paramMap.add("maxPrice", productQuery.getMaxPrice());

        List<ProductVo> list = restTemplate.postForObject(url, productQuery, List.class);

        System.out.println(list);

        return list;
    }
}
